﻿using BZhanHengjicvWPF.Common;
using BZhanHengjicvWPF.Common.Models.Events;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace BZhanHengjicvWPF.Extensions
{
    public static class DialogExtensions
    {
        /// <summary>
        /// 访问主机
        /// </summary>
        /// <param name="dialogHost">指定的dialoghost对话主机</param>
        /// <param name="title">标题</param>
        /// <param name="content">访问内容</param>
        /// <param name="dialogHostName">会话主机名称（唯一）</param>
        /// <returns></returns>
        public static async Task<IDialogResult> Question(this IDialogHostService dialogHost, string title, string content, string dialogHostName = "Root")
        {
            DialogParameters param = new DialogParameters();
            param.Add("Title", title);
            param.Add("Content", content);
            param.Add("dialogHostName", dialogHostName);
            var dialogResult = await dialogHost.ShowDialog("MsgView", param, dialogHostName);
            return dialogResult;
        } 
        
        /// <summary>
        /// 注册等待消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="action"></param>
        public static void Register(this IEventAggregator aggregator, Action<UpdateModel> action)
        {
            aggregator.GetEvent<UpdateLoadingEvent>().Subscribe(action);
        }

        /// <summary>
        /// 推送等待消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="updateModel"></param>
        public static void UpdateLoading(this IEventAggregator aggregator, UpdateModel updateModel)
        {
            aggregator.GetEvent<UpdateLoadingEvent>().Publish(updateModel);
        }
 
        /// <summary>
        /// 注册提示消息
        /// </summary>
        public static void RegisterMessage(this IEventAggregator aggregator, Action<MessageModel> action,string filterName="Main")
        {
            aggregator.GetEvent<MessageEvent>()
                .Subscribe(action, ThreadOption.PublisherThread, true, (m) => { return m.Filter.Equals(filterName); });
        }
        /// <summary>
        /// 发送提示消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="message"></param>
        /// <param name="filterName"></param>
        public static void SendMessage(this IEventAggregator aggregator, string message, string filterName = "Main")
        {
            aggregator.GetEvent<MessageEvent>().Publish(
                new MessageModel() { Filter = filterName, Message = message });
        }

    }
}
